﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 飞行道具
/// </summary>
public class FightProp : OBJBase, IObjectMove, ILookToCamera
{

    GameObject obj;

    GameObject anima;
    GameObject rota;
    GameObject look;
    public GameObject Obj { get => obj; set => obj = value; }
    public bool IsMove { get => isMove; set => isMove = value; }

    Vector3 dir;
    float time;
    bool isMove;
    public FightProp()
    {
        Obj = GameObject.Instantiate<GameObject>(ResourceObject("ObjectRes/PropTest"), Vector3.zero, Quaternion.identity);
        rota = Obj.transform.Find("Rotation").gameObject;
        anima = Obj.transform.Find("Rotation/PropAnima").gameObject;
        LookToCamera();
        MonoMgr.GetInstance().AddUpdateListener(Move);
        MonoMgr.GetInstance().AddUpdateListener(DeadTiem);
        time = 10;
    }
    public FightProp(string url)
    {

    }
    /// <summary>
    /// 设置移动的方向
    /// </summary>
    /// <param name="dir">方向向量</param>
    /// <param name="end">鼠标点击的最终位置</param>
    public void SetMoveDIr(Vector3 dir, Vector3 end)
    {

        //计算图片旋转的角度
        this.dir = dir;
        Vector3 endSen = Camera.main.WorldToViewportPoint(end);
        Vector3 objSen = Camera.main.WorldToViewportPoint(Obj.transform.position);


        float shijichangdu = Mathf.Abs(Vector3.Distance(new Vector3(endSen.x, endSen.y, 0), new Vector3(objSen.x, objSen.y, 0)));
        float duibian = Mathf.Abs(endSen.y - objSen.y);
        float fudu = duibian / shijichangdu;
        float jiaodu = Mathf.Asin(fudu) * Mathf.Rad2Deg;


        //根据区域旋转图片的方向
        if (endSen.x >= objSen.x && endSen.y >= objSen.y) //第一
        {
            //Debug.Log("第1");
            anima.transform.localEulerAngles = new Vector3(0, 0, jiaodu);
        }
        else if (endSen.x < objSen.x && endSen.y >= objSen.y)
        {
            //Debug.Log("第2");
            anima.transform.localEulerAngles = new Vector3(0, 0, -jiaodu);
            anima.GetComponent<SpriteRenderer>().flipX = true;
        }
        else if (endSen.x < objSen.x && endSen.y < objSen.y)//第三
        {
            //  Debug.Log("第3");
            anima.GetComponent<SpriteRenderer>().flipX = true;
            anima.transform.localEulerAngles = new Vector3(0, 0, jiaodu);
        }
        else if (endSen.x >= objSen.x && endSen.y < objSen.y)
        {
            //  Debug.Log("第4");
            anima.transform.localEulerAngles = new Vector3(0, 0, -jiaodu);
        }


    }
    void DeadTiem()
    {
        time = time - 1f * Time.deltaTime;
        if (time < 0)
        {
            ObjDestroy();
        }
    }

    public void Move()
    {
        if (IsMove)
        {
            Obj.transform.Translate(dir.normalized * 1 * Time.deltaTime);
        }
        for (int i = 0; i < EnemyController.GetInstance().enemyList.Count; i++)
        {
            if (Vector3.Distance(EnemyController.GetInstance().enemyList[i].Obj.transform.position, Obj.transform.position) < 0.1f)
            {
                EnemyController.GetInstance().enemyList[i].Recycle();
                EnemyController.GetInstance().enemyList.RemoveAt(i);
                ObjDestroy();
            }
        }
    }

    public void LookToCamera()
    {
        rota.transform.localEulerAngles = new Vector3(CameraContorller.GetInstance().LookCamera, 0, 0);
    }

    public void ObjDestroy()
    {
        MonoMgr.GetInstance().RemoveUpdateListener(DeadTiem);
        MonoMgr.GetInstance().RemoveUpdateListener(Move);
        GameObject.Destroy(Obj);
        PropController.GetInstance().PropList.Remove(this);
    }
}
